/*
 *  testClassData.cpp
 *  kangmodb
 *
 *  Created by 강모 김 on 11. 4. 25..
 *  Copyright 2011 강모소프트. All rights reserved.
 *
 */

#include "gtest/gtest.h"

#include "kangmodb.h"
#include "testCommon.h"

/** @brief Test stgData class 
 */
class ClassDataTest : public testing::Test {
protected:  // You should make the members protected s.t. they can be
	// accessed from sub-classes.
	
	// virtual void SetUp() will be called before each test is run.  You
	// should define it if you need to initialize the varaibles.
	// Otherwise, this can be skipped.
	virtual void SetUp() {
	}
	
	// virtual void TearDown() will be called after each test is run.
	// You should define it if there is cleanup work to do.  Otherwise,
	// you don't have to provide it.
	//
	virtual void TearDown() {
	}
protected:
};

typedef struct data_struct
{
	int  field1;
	char field2[100];
} data_struct;

data_struct d = {1004, "Hyunsook Cho"};

void verify_data(stgData * data)
{
	EXPECT_EQ( ((data_struct*)data->getData())->field1, 1004 );
	EXPECT_TRUE( strcmp(((data_struct*)data->getData())->field2, "Hyunsook Cho") == 0 );
	EXPECT_EQ(data->getLength(), sizeof(d));
}

TEST_F(ClassDataTest, Basic) {
	stgData data( &d, sizeof(d));
	verify_data( & data );
}

TEST_F(ClassDataTest, CopyAssignment) {
	stgData dst;
	stgData src(&d, sizeof(d));

	// Test copy assignment operator.
	dst = src;
	
	// Check if the dst object has the same data with the original source object.
	verify_data( & dst );
}

static int compare(const char * data1_str, const stgValueLength data1_len, const char * data2_str, const stgValueLength data2_len)
{
	stgData data1(data1_str, data1_len);
	stgData data2(data2_str, data2_len);
	
	return data1.compare(data2);
}  

TEST_F(ClassDataTest, DataComparison) {
	// Basic Tests
	EXPECT_TRUE( compare("a", 1, "a", 1) == 0 );
	EXPECT_TRUE( compare("ab", 2, "a", 1) > 0 );
	EXPECT_TRUE( compare("a", 1, "ab", 2) < 0 );

	// Having more characters after the given length does not affect the result of comparison
	EXPECT_TRUE( compare("a0", 1, "a0", 1) == 0 );
	EXPECT_TRUE( compare("ab0", 2, "a0", 1) > 0 );
	EXPECT_TRUE( compare("a0", 1, "ab0", 2) < 0 );

	// Having more characters after the given length does not affect the result of comparison
	EXPECT_TRUE( compare("a0", 1, "a1", 1) == 0 );
	EXPECT_TRUE( compare("ab0", 2, "a1", 1) > 0 );
	EXPECT_TRUE( compare("a0", 1, "ab1", 2) < 0 );

	// Having more characters after the given length does not affect the result of comparison
	EXPECT_TRUE( compare("a1", 1, "a0", 1) == 0 );
	EXPECT_TRUE( compare("ab1", 2, "a0", 1) > 0 );
	EXPECT_TRUE( compare("a1", 1, "ab0", 2) < 0 );
}

